package com.zhangxi.springbootwebzhangxi.controller;

import com.zhangxi.springbootwebzhangxi.pojo.Result;
import com.zhangxi.springbootwebzhangxi.util.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * ClassName: UploadController
 * Package: com.zhangxi.springbootwebzhangxi.controller
 */
@RestController
@Slf4j
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result uploadTest(String username,
                             Integer age,
                             @RequestParam("image") MultipartFile file) throws IOException {
        log.info("文件上传：{},{},{}", username, age, file);
//        //获取原始文件名
//        String originalFilename = file.getOriginalFilename();
//        //构建新的文件名
//        String extname = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String newFilename = UUID.randomUUID().toString()+extname;
//
////将文件存储在服务器的磁盘目录
//        file.transferTo(new File("E:\\资料\\zhangxi\\12.SpringBoot\\3.SpringBootWeb案例02\\文件上传" + newFilename));
//        return Result.success();
        //调用阿里云OSS工具类，将上传上来的文件存入阿里云
        String url = aliOSSUtils.upload(file);
        //将图片上传完成后的url返回，用于浏览器回显展示
        return Result.success(url);
    }
}
